﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Putta.KeyPass.Resources;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Navigation;

namespace Putta.KeyPass
{
    /// <summary>
    /// Represent the Windows Phone Application.
    /// </summary>
    public partial class App : Application
    {
        private AppSettings settings;
        private PhoneApplicationFrame rootFrame;
        private string liveClientId;
        private bool beingResume;

        /// <summary>
        /// Initialize a new <see cref="App"/>.
        /// </summary>
        public App()
        {
            UnhandledException += OnUnhandledException;

            // Initialize Application.
            InitializeComponent();

            this.settings = (AppSettings)Resources["AppSettings"];
            this.liveClientId = (string)Resources["LiveClientId"];

            this.rootFrame = new PhoneApplicationFrame();
            this.rootFrame.Navigating += OnRootFrameNavigating;
            this.rootFrame.Navigated += OnFirstNavigationComplete;
            this.rootFrame.Navigated += OnRootFrameNavigated;
            this.rootFrame.NavigationFailed += OnRootFrameNavigationFailed;
            this.rootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);
            this.rootFrame.FlowDirection = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);

            // Show graphics profiling information while debugging.
            if (Debugger.IsAttached)
            {
                Application.Current.Host.Settings.EnableFrameRateCounter = true;
                PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            }
        }

        /// <summary>
        /// Get Client Identifier for Microsoft Live.
        /// </summary>
        /// <value>
        /// Live Client Identifier; cannot be <c>null</c>.
        /// </value>
        public string LiveClientId
        {
            get { return this.liveClientId; }
        }

        /// <summary>
        /// Get the Application's settings.
        /// </summary>
        /// <value>
        /// Application's settings; cannot be <c>null</c>.
        /// </value>
        public AppSettings Settings
        {
            get { return this.settings; }
        }

        private void OnApplicationActivated(object sender, ActivatedEventArgs e)
        {
        }

        private void OnApplicationClosing(object sender, ClosingEventArgs e)
        {
        }

        private void OnApplicationDeactivated(object sender, DeactivatedEventArgs e)
        {
        }

        private void OnApplicationLaunching(object sender, LaunchingEventArgs e)
        {
        }

        private void OnFirstNavigationComplete(object sender, NavigationEventArgs e)
        {
            this.RootVisual = this.rootFrame;
            this.rootFrame.Navigated -= OnFirstNavigationComplete;
        }

        private void OnRootFrameNavigated(object sender, NavigationEventArgs e)
        {
            this.beingResume = (e.NavigationMode == NavigationMode.Reset);
        }

        private void OnRootFrameNavigating(object sender, NavigatingCancelEventArgs e)
        {
            if (this.beingResume)
            {
                if (e.IsCancelable)
                    e.Cancel = true;
                this.beingResume = false;
            }
        }

        private void OnRootFrameNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (Debugger.IsAttached)
                Debugger.Break();
        }

        private void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (Debugger.IsAttached)
                Debugger.Break();
        }
    }
}